from tkinter import *
from tkinter.ttk import *
import tkinter.messagebox as messageBox
# import tkinter.tix

from ...utils.workflow_manager import WorkFlow

from ...utils.loggers import record_logger
from ...utils.utils import singleton

from .app import RemoteStartWorkFlow


@singleton
class RemoteStartTest:
    def  __init__(self, master) -> None:
        self.MASTER = master
        self.stepManager = RemoteStartWorkFlow(mainView=master, parentView=self, stepCounts=5)
        # print(self.stepManager.mainView, 'self.stepManager.mainView')
        # print(self.stepManager, 'self.stepManager.mainView')
        
        #初始化窗口
        self.initCurWindow()

        self.stepManager.run()

    def initCurWindow(self):
        self.curWindow = Toplevel(self.MASTER)
        self.curWindow.geometry('900x700+600+200')
        self.curWindow.title('远程启动操作台')

        # Frame 初始化
        self.initUserWindowFrame2()
        self.initUserWindowFrame3()
        self.initUserWindowFrame6()

    def initUserWindowFrame2(self):
        # 步骤可视化
        self.userWindowFrame2 = Frame(self.curWindow)
        self.userWindowFrame2.grid(row=2, column=1, sticky='w', columnspan=2, padx=20, pady=20)
        if 1:
            self.userWindowFrame2_1 = Frame(self.userWindowFrame2, relief='groove')
            self.userWindowFrame2_1.grid(row=1, column=1)
            # FIXME: 通过循环，利用setattr和getattr动态操作属性，创建组件,减少代码量
            for i in range(8):
                labelIndex= 'userWindowFrame2_1StepLabel' + str(i)
                stepIndex = 'step' + str(i)
                columnIndex = i+1
                setattr(self, labelIndex, Label(self.userWindowFrame2_1, text=stepIndex))
                getattr(self, labelIndex).grid(row=2, column=columnIndex, padx=10, pady=10)

            def refreshStep(num):
                maxStep = self.stepManager.stepCounts
                # print('maxStep:', maxStep)
                for i in range(maxStep):
                    labelIndex= 'userWindowFrame2_1StepLabel' + str(i)
                    getattr(self, labelIndex).configure(background='gray')

                for i in range(num + 1):
                    labelIndex= 'userWindowFrame2_1StepLabel' + str(i)
                    getattr(self, labelIndex).configure(background='blue')
            # [ ]: 后续删除
            setattr(self, 'refreshStep', refreshStep)
            # self.refreshStep(5)

        self.userWindowFrame2_1 = Frame(self.curWindow)
        self.userWindowFrame2_1.grid(row=2, column=2, sticky='e', padx=20, pady=20)

        self.userWindowFrame2_1_Btn01 = Button(self.userWindowFrame2_1,text='重置步数0', command=self.stepManager.stepReset)
        self.userWindowFrame2_1_Btn01.grid(row=1, column=1, padx=10, pady=10)
        
    def refreshStep(self,num):
        maxStep = self.stepManager._stepCounts
        # print('maxStep:', maxStep)
        for i in range(maxStep):
            labelIndex= 'userWindowFrame2_1StepLabel' + str(i)
            getattr(self, labelIndex).configure(background='gray')

        for i in range(num + 1):
            labelIndex= 'userWindowFrame2_1StepLabel' + str(i)
            getattr(self, labelIndex).configure(background='blue')

    def initUserWindowFrame3(self):
        # step操作流程
        self.userWindowFrame3 = LabelFrame(self.curWindow, text='操作流程',  relief='raised')
        self.userWindowFrame3.grid(row=3, column=1, padx=20, pady=20, sticky='nsw')


        if 1:

            self.userWindowFrame3_Label1 = Label(self.userWindowFrame3, text='stepFlow')
            self.userWindowFrame3_Label1.grid(row=1, column=1,columnspan=3, padx=20, pady=20)


            # 开启测试和关闭测试总控制
            if 0:
                self.userWindowFrame3StepSwitchFrame = Frame(self.userWindowFrame3, relief='groove')
                self.userWindowFrame3StepSwitchFrame.grid(row=2, column=1, padx=20, pady=20)
                self.userWindowFrame3_Btn1 = Button(self.userWindowFrame3StepSwitchFrame, text='测试启动', command=self.stepManager.run)  
                self.userWindowFrame3_Btn1.grid(row=2, column=1, padx=20, pady=20)
                # [ ]:增加停止测试的btn按钮，尝试终止循环的方法让子线程的任务运行完毕
            # 手动启动测试
            # self.stepManager.run()
            

            # 循环step明细
            if 1:
                self.userWindowFrame3StepsFrame = Frame(self.userWindowFrame3, relief='groove')
                self.userWindowFrame3StepsFrame.grid(row=3, column=1, padx=20, pady=20, sticky='nsw')

                # step 0 UI
                if 1:
                    self.userWindowFrame3StepsFrameStep0Frm = LabelFrame(self.userWindowFrame3StepsFrame,text='step0', relief='groove') 
                    self.userWindowFrame3StepsFrameStep0Frm.grid(row=1, column=1, padx=10, pady=10, sticky='w')
                    self.userWindowFrame3StepsFrameStep0Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep0Frm, text='启动检测', command=lambda : self.stepManager.setStepOverFlag(0))
                    self.userWindowFrame3StepsFrameStep0Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                if 0:
                    self.userWindowFrame3StepsFrameStep1Frm = Frame(self.userWindowFrame3StepsFrame, relief='groove')
                    self.userWindowFrame3StepsFrameStep1Frm.grid(row=2, column=1, padx=10, pady=10)
                    self.userWindowFrame3StepsFrameStep1Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep1Frm, text='step1')
                    self.userWindowFrame3StepsFrameStep1Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                if 0:
                    self.userWindowFrame3StepsFrameStep2Frm = Frame(self.userWindowFrame3StepsFrame, relief='groove')
                    self.userWindowFrame3StepsFrameStep2Frm.grid(row=3, column=1, padx=10, pady=10)
                    self.userWindowFrame3StepsFrameStep2Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep2Frm, text='step2')
                    self.userWindowFrame3StepsFrameStep2Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                if 1:
                    self.userWindowFrame3StepsFrameStep3Frm = LabelFrame(self.userWindowFrame3StepsFrame,text='step3', relief='groove')
                    self.userWindowFrame3StepsFrameStep3Frm.grid(row=4, column=1, padx=10, pady=10, sticky='w')
                    # self.userWindowFrame3StepsFrameStep3Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep3Frm, state=DISABLED,text='step3')
                    # self.userWindowFrame3StepsFrameStep3Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                    self.userWindowFrame3StepsFrameStep3Frm_qidongqueren = Label(self.userWindowFrame3StepsFrameStep3Frm, text='发动机启动确认', foreground='gray')
                    self.userWindowFrame3StepsFrameStep3Frm_qidongqueren.grid(row=1, column=2,padx=10, pady=10)
                if 0:
                    self.userWindowFrame3StepsFrameStep4Frm = Frame(self.userWindowFrame3StepsFrame, relief='groove')
                    self.userWindowFrame3StepsFrameStep4Frm.grid(row=5, column=1, padx=10, pady=10)
                    self.userWindowFrame3StepsFrameStep4Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep4Frm, text='step4')
                    self.userWindowFrame3StepsFrameStep4Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                if 0:
                    self.userWindowFrame3StepsFrameStep5Frm = LabelFrame(self.userWindowFrame3StepsFrame,text='step5', relief='groove')
                    self.userWindowFrame3StepsFrameStep5Frm.grid(row=6, column=1, padx=10, pady=10, sticky='w')
                    # self.userWindowFrame3StepsFrameStep5Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep5Frm, text='step5', state=DISABLED)
                    # self.userWindowFrame3StepsFrameStep5Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                    self.userWindowFrame3StepsFrameStep5Frm_label1 = Label(self.userWindowFrame3StepsFrameStep5Frm, text='剩余时间:')
                    self.userWindowFrame3StepsFrameStep5Frm_label1.grid(row=2, column=1,padx=0, pady=10)
                    self.userWindowFrame3StepsFrameStep5Frm_label2 = Label(self.userWindowFrame3StepsFrameStep5Frm, text='**', font=('Arial', 32))
                    self.userWindowFrame3StepsFrameStep5Frm_label2.grid(row=2, column=2,padx=0, pady=0)
                if 0:
                    self.userWindowFrame3StepsFrameStep6Frm = LabelFrame(self.userWindowFrame3StepsFrame,text='step6', relief='groove')
                    self.userWindowFrame3StepsFrameStep6Frm.grid(row=7, column=1, padx=10, pady=10, sticky='w')
                    self.userWindowFrame3StepsFrameStep6Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep6Frm, text='加热温度确认',state='disabled')
                    self.userWindowFrame3StepsFrameStep6Frm_btn1.grid(row=1, column=1,padx=10, pady=10)
                if 0:
                    self.userWindowFrame3StepsFrameStep7Frm = Frame(self.userWindowFrame3StepsFrame, relief='groove')
                    self.userWindowFrame3StepsFrameStep7Frm.grid(row=8, column=1, padx=10, pady=10)
                    self.userWindowFrame3StepsFrameStep7Frm_btn1 = Button(self.userWindowFrame3StepsFrameStep7Frm, text='step7', command=self.stepManager.step7SetFlag)
                    self.userWindowFrame3StepsFrameStep7Frm_btn1.grid(row=1, column=1,padx=10, pady=10)

                    pass

    def initUserWindowFrame6(self):
        # 测试结果显示框
        self.userWindowFrame6_2 = Frame(self.curWindow)
        self.userWindowFrame6_2.grid(row=3,column=2, padx=20,pady=20)
        if 1:

            self.userWindowFrame6recordListboxScroll_2 = Scrollbar(self.userWindowFrame6_2, orient=VERTICAL)
            self.userWindowFrame6recordListboxScroll_2.grid(row=2, column=11, sticky="WS")
            self.userWindowFrame6recordListbox_2 = Listbox(
                self.userWindowFrame6_2,
                font=("Arial", 12),
                height=25,
                width=50,
                yscrollcommand=self.userWindowFrame6recordListboxScroll_2.set,
            )
            self.userWindowFrame6recordListbox_2.grid(row=2, column=0, columnspan=11, sticky="nw")
            self.userWindowFrame6recordListboxScroll_2.config(command=self.userWindowFrame6recordListbox_2.yview)
            self.userWindowFrame6recordClearBtn_2 = Button(
                self.userWindowFrame6_2,
                text="清空测试记录",
                command=lambda: self.userWindowFrame6recordListbox_2.delete(0,END),
            )
            self.userWindowFrame6recordClearBtn_2.grid(row=2, column=12, padx=2, pady=1)
            pass


    def sendMsg_checkRecv(self, sendId=None, sendData=None,recvId=None,recvData=None, delay=0.5, tryCounts=5):
        '''
        发送信号，返回True或者False，在WorkFlow中通过返回值进行下一步的执行逻辑
        '''

        def sendMsg():
            if sendId is not None and sendData is not None:
                self.MASTER.sendcan1_manual(sendId, sendData)
            else:
                raise 'no sendId or no sendData'
        sendMsg()
        return True

    def display_log(self, log):
        self.userWindowFrame6recordListbox_2.insert(END, log)
        self.userWindowFrame6recordListbox_2.see(END)
    def display_warning(self, warning):
        self.display_log(warning)
        self.userWindowFrame6recordListbox_2.itemconfig('end', background='pink')
    def display_record(self, msg):
        record_logger.info(msg)




def app_window(master):
    '''
        座椅检测弹出窗口
    '''
    RemoteStartTest(master)
